package com.nekonomics
package persistence.repository

import domain.entity.Sku

import com.nekonomics.domain.auxiliary.SimpleOrderItem
import com.nekonomics.types.OrderStatus
import com.nekonomics.utils.magic.SqlMagic
import doobie.free.connection.ConnectionIO
import doobie.*
import doobie.implicits.*
import zio.*



trait OrderItemRepo {
  def batchInsert(items: List[SimpleOrderItem]): ConnectionIO[Int]
}

object OrderItemRepo {
  val live: ULayer[OrderItemRepo] = ZLayer.succeed(new OrderItemRepoLive)
}



private class OrderItemRepoLive extends OrderItemRepo {
  override def batchInsert(items: List[SimpleOrderItem]): ConnectionIO[Int] = {
    val fields = SqlMagic.fieldSnakeNames4Sql[SimpleOrderItem]
    val placeholders = SqlMagic.fieldHolder4Sql[SimpleOrderItem]
    Update[SimpleOrderItem](s"insert into public.order_item ($fields) values $placeholders")
      .updateMany(items)
  }
}